package com.sgb.kjwl.view.widget.member;

import android.content.Context;
import android.graphics.Color;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.sgb.kjwl.R;
import com.sgb.kjwl.view.widget.MaxHeightRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.ScreenUtil;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.List;

public class CommonPopWindow extends BasePopWindow<CommonPopEntity> {

    private BaseRecycleAdapter<CommonPopEntity> mAdapter;

    private int mCurPos = 0;
    private LinearLayout linearLayout;
    private final boolean isUserCenter;

    public CommonPopWindow(Context context) {
        super(context);
        this.isUserCenter = false;
        updateBack();
    }

    public CommonPopWindow(Context context, boolean isUserCenter) {
        super(context);
        this.isUserCenter = isUserCenter;
        updateBack();
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_common;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        linearLayout = holder.getPop().findViewById(R.id.common_layout);
        if (holder.getPop().findViewById(R.id.goods_type_list) instanceof MaxHeightRecyclerView) {
            MaxHeightRecyclerView recyclerView = holder.getPop().findViewById(R.id.goods_type_list);
            TextView tv_next = holder.getPop().findViewById(R.id.tv_next);
            TextView tv_ok = holder.getPop().findViewById(R.id.tv_ok);
            holder.getPop().findViewById(R.id.bg_pop_gray_layout).setOnClickListener(view -> dissMiss());
            tv_next.setOnClickListener(v -> {

            });
            tv_ok.setOnClickListener(v -> {

            });
            int height = ScreenUtil.dip2px(45);
            recyclerView.setMaxHeight(height * 5 + height / 2);
            updateChildRecycler(recyclerView);
        }
    }

    public void updateChildRecycler(RecyclerView recyclerView) {
        mAdapter = new BaseRecycleAdapter<CommonPopEntity>(mContext, R.layout.popwindow_common_item, null) {
            @Override
            public void convert(ItemViewHolder holder, CommonPopEntity entity) {
                RelativeLayout relativeLayout = holder.itemView.findViewById(R.id.item_bg);
                TextView textView = holder.itemView.findViewById(R.id.tv_name);
                textView.setText(entity.getPickerViewText());
                if (holder.getItem().isSelected()) {
                    relativeLayout.setBackground(null);
                    textView.setBackgroundResource(R.drawable.shape_member_left_press);
                    textView.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.color_main));
                } else {
                    relativeLayout.setBackground(null);
                    textView.setBackgroundResource(R.drawable.shape_member_left);
                    textView.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.color_666666));
                }
            }
        };
        mAdapter.setItemSelect(true);
        mAdapter.setDefItem(mCurPos);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClick(new AdapterOnItemClick<CommonPopEntity>() {
            @Override
            public void onItemClick(CommonPopEntity entity, int position) {
                mCurPos = position;
                if (onResultClick != null) {
                    onResultClick.result(entity, null, position);
                }
                dissMiss();
            }
        });
    }

    private void updateBack() {
        if (isUserCenter) {
            linearLayout.setBackgroundColor(linearLayout.getResources().getColor(R.color.color_white));
        } else {
            linearLayout.setBackgroundColor(Color.parseColor("#e6F5F5F5"));
        }
    }

    public int getCurPos() {
        return mCurPos;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        onDisMiss.disMiss();
    }

    public void setDefItem(int defItem) {
        mCurPos = defItem;
        mAdapter.setDefItem(defItem);
    }

    public void setData(List<CommonPopEntity> list) {
        mAdapter.setDatas(list);
    }

    public void setData(int defItem, List<CommonPopEntity> list) {
        mCurPos = defItem;
        mAdapter.setDatas(list);
        mAdapter.setDefItem(defItem);
    }

}
